{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 对数几率回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实验内容\n",
    "1. 使用对数几率回归完成垃圾邮件分类问题和Dota2结果预测问题。\n",
    "2. 计算十折交叉验证下的精度(accuracy)，查准率(precision)，查全率(recall)，F1值。\n",
    "\n",
    "## 评测指标  \n",
    "1. 精度\n",
    "2. 查准率\n",
    "3. 查全率\n",
    "4. F1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "spambase = np.loadtxt('data/spambase/spambase.data', delimiter = \",\")\n",
    "dota2results = np.loadtxt('data/dota2Dataset/dota2Train.csv', delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(spambase)\n",
    "type(dota2results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 导入模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import precision_score\n",
    "from sklearn.metrics import recall_score\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.model_selection import cross_val_predict\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model.logistic import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split, cross_val_score\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from matplotlib.font_manager import FontProperties"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 提取数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里的spamx和dota2x包含了数据集内所有的特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "spamx = spambase[:, :57]\n",
    "spamy = spambase[:, 57]\n",
    "\n",
    "dota2x = dota2results[:, 1:]\n",
    "dota2y = dota2results[:, 0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 训练并预测"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请你完成两个模型使用全部特征的训练与预测，并将预测结果存储起来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4601L,)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# YOUR CODE HERE\n",
    "\n",
    "\n",
    "model = LogisticRegression()\n",
    "prediction = cross_val_predict(model, spamx, spamy, cv = 10)\n",
    "prediction.shape\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 评价指标的计算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请你计算两个模型的四项指标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('accuracy:', 0.38578569876113888)\n",
      "('precision:', 0.90519055280885152)\n",
      "('recall:', 0.88418735960172423)\n",
      "('F1:', 0.89393015931768116)\n"
     ]
    }
   ],
   "source": [
    "# YOUR CODE HERE\n",
    "\n",
    "print('accuracy:',np.mean(prediction))\n",
    "#准确率=查准率\n",
    "precisions = cross_val_score(model, spamx, spamy, cv=10, scoring='precision')\n",
    "print('precision:', np.mean(precisions))\n",
    "#召回率=查全率\n",
    "recalls = cross_val_score(model, spamx, spamy, cv=10, scoring='recall')\n",
    "print('recall:', np.mean(recalls))\n",
    "\n",
    "f1s = cross_val_score(model, spamx, spamy, cv=10, scoring='f1')\n",
    "print('F1:', np.mean(f1s))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('accuracy:', 0.17450620615218565)\n",
      "('precision:', 0.60663107286363671)\n",
      "('recall:', 0.67660221560198797)\n"
     ]
    }
   ],
   "source": [
    "model = LogisticRegression()\n",
    "prediction = cross_val_predict(model, dota2x, dota2y, cv = 10)\n",
    "prediction.shape\n",
    "\n",
    "\n",
    "print(\"accuracy:\",np.mean(prediction))\n",
    "#准确率=查准率\n",
    "precisions = cross_val_score(model, dota2x, dota2y, cv=10, scoring='precision')\n",
    "print('precision:', np.mean(precisions))\n",
    "#召回率=查全率\n",
    "recalls = cross_val_score(model, dota2x, dota2y, cv=10, scoring='recall')\n",
    "print('recall:', np.mean(recalls))\n",
    "\n",
    "f1s = cross_val_score(model, dota2x, dota2y, cv=10, scoring='f1')\n",
    "print('F1:', np.mean(f1s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 双击此处填写\n",
    "\n",
    "数据集|精度|查准率|查全率|F1\n",
    "-|-|-|-|-\n",
    "spambase | 17.45 | 90.52 | 88.42 | 89.39\n",
    "dota2Results | 17.45 | 60.67 | 67.66 | 63.97"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. 选做：尝试对特征进行变换、筛选、组合后，训练模型并计算十折交叉验证后的四项指标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# YOUR CODE HERE\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 双击此处填写\n",
    "1. 模型1的处理流程：\n",
    "2. 模型2的处理流程：\n",
    "3. 模型3的处理流程:\n",
    "\n",
    "模型|数据集|精度|查准率|查全率|F1\n",
    "-|-|-|-|-|-\n",
    "模型1 | 数据集 | 0.0 | 0.0 | 0.0 | 0.0\n",
    "模型2 | 数据集 | 0.0 | 0.0 | 0.0 | 0.0\n",
    "模型3 | 数据集 | 0.0 | 0.0 | 0.0 | 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
